from toee import *

from utilities import *

from __main__ import game

from Co8 import *

from py00439script_daemon import record_time_stamp, get_v, set_v, npc_set, npc_unset, npc_get, tsc, within_rect_by_corners

from council import council_time, traders_awol

# Contained in this script:
#  -Shop map: Money script (for correct amount of money per PHB
#  -Hommlet exterior: Remove the goblin with the ring, council scripts
#  -Burne's Tower: Council related scripting
#  -Deklo Grove: ring bearer spawner (according to alignment)
#  -Moathouse: Big Three encounter, Frog with ring spawner
#  -Ogre Cave: Orc prespawn
#  -Temple exterior: Flags Burnt Farm, Ruined Temple House, Temple Tower visited
#  -Temple lvl2: Fix for giving Countess Tillahi instructions if you've been taken blindfolded into the Temple
#  -Wilderness encounter map: does the trader thing

#obj_f_npc_pad_i_3 bit flags:
#1-5 - Burne related
# 10 - indicates "have made Hommletans go home" (the ones who spawn following the council trap thing)
# 11 - indicates "have spawned extra spawners from original spawner at 711, 521"

def san_heartbeat( attachee, triggerer ):
	attachee.critter_flag_set( OCF_MUTE )
	attachee.object_flag_unset( OF_OFF )
	cur_map = game.party[0].map
	if (cur_map == 5001): 
	#### Hommlet Exterior 	###
		council_heartbeat( attachee )
		#game.particles( "sp-Dimension Door", attachee )
		set_v('Test559',3)

		################################
		# Spawner Propagation scripting
		################################
		# Start by finding nearest PC
		chosen_one = game.party[0]
		ddd = 30000
		for pc in game.party:
			if pc.type == obj_t_pc:
				if attachee.distance_to(pc) < ddd:
					ddd = attachee.distance_to(pc)
					chosen_one = pc
		gen_spawner = find_npc_near(chosen_one, 14706)

		abbort = 0
		if ddd >= 74:
			#game.particles( 'Orb-Summon-Water-Elemental', chosen_one )
			dummy = 1
			set_v('Test559',1)
		if gen_spawner == OBJ_HANDLE_NULL:
			#game.particles( 'Orb-Summon-Fire-Elemental', chosen_one )
			dummy = 1
			game.global_vars[559] = 2
		if gen_spawner == OBJ_HANDLE_NULL and ddd >= 75:
			moshe = game.obj_create(14706, chosen_one.location)
			moshe.object_flag_unset( OF_OFF )
			#game.particles( 'sp-Bless Water', chosen_one )
			game.global_vars[559] = 4

	elif cur_map == 5016:
	#Burne's Tower Main Hall
		game.particles( 'sp-Bless Water', attachee )
		council_heartbeat( attachee )
		c_time = council_time()
		burne = find_npc_near(attachee, 8054)
		rufus = find_npc_near(attachee, 14006)
		a = attachee.obj_get_int( obj_f_npc_pad_i_3)

		# Spawner Internal flags (used bitwise). Meanings:
		# a & 1 - indicates Burne is gonna do his "gone to council" dialogue thing - wait a little before switching him off (this flag is lowered immediately after he speaks to enable making him disappear)
		# a & 2 - indicates arrest by badger has already gone on
		# a & 4 - indicates 'ho there! you broke out of prison' has fired
		# a & 8 - indicates Burne has done his "gone to council" dialogue thing

		b = burne.object_flags_get()
		rep29 = game.party[0].reputation_has(29) #Lawbreaker
		rep27 = game.party[0].reputation_has(27) #Rabble-rouser
		rep30 = game.party[0].reputation_has(30) #Convict
		ggv435 = get_v(435) #Council state
		if rep30 and npc_get(attachee,3) == 0 and burne != OBJ_HANDLE_NULL:
			##you're a convict; means you broke out
			game.particles( 'Orb-Summon-Fire-Elemental', attachee )
		elif (game.global_vars[730] == 0 and burne.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active() ):
			burne.cast_spell(spell_mage_armor, burne)
			burne.spells_pending_to_memorized()
			game.global_vars[730] = 1

	elif (cur_map == 5123):	# Shop Map
		money_script()
		attachee.destroy()	
		
	elif (attachee.map == 5093):
		game.areas[7] = 1
		attachee.destroy()
		
	elif (attachee.map == 5112):
		game.areas[11] = 1
		attachee.destroy()
		
	elif (attachee.map == 5113):
		game.areas[12] = 1
		attachee.destroy()
		
	elif (attachee.map == 5091):	# Moathouse Cave Exit
		game.areas[8] = 1
		if not (is_follower(8002) or is_follower(8004) or is_follower(8005) or is_follower(8010) or game.party_alignment == LAWFUL_EVIL):
			if ((game.global_flags[44] != 1) and (game.global_flags[45] != 1) and (game.global_flags[700] != 1) and (game.global_flags[37] == 1) and (game.global_flags[283] == 0)):
				Big_Three_Ambush_1(attachee)
				attachee.destroy()

	elif (attachee.map == 5002):	# Moathouse Courtyard & Back Door
        	for leader in game.party:
            		if within_rect_by_corners(leader, 437, 433, 434, 460) == 0:        # Courtyard
               			banditcheck = find_npc_near(attachee,14070)
                		if (banditcheck == OBJ_HANDLE_NULL):
                    			if not (is_follower(8002) or is_follower(8004) or is_follower(8005) or is_follower(8010) or game.party_alignment == LAWFUL_EVIL):
                        			if ((game.global_flags[44] != 1) and (game.global_flags[45] != 1) and (game.global_flags[700] != 1) and (game.global_flags[37] == 1) and (game.global_flags[283] == 0)):
                            				Big_Three_Ambush_2(attachee)
							attachee.destroy()
        	    	else:    # Back Door
                		if not (is_follower(8002) or is_follower(8004) or is_follower(8005) or is_follower(8010) or game.party_alignment == LAWFUL_EVIL):
                    			if ((game.global_flags[44] != 1) and (game.global_flags[45] != 1) and (game.global_flags[700] != 1) and (game.global_flags[37] == 1) and (game.global_flags[283] == 0)):
                        			Big_Three_Ambush_3(attachee)
						attachee.destroy()

	if (attachee.map == 5094 and game.global_vars[996] == 5):	# Emridy Meadows
		slave_traders(attachee)
		attachee.destroy()

	return RUN_DEFAULT


def council_heartbeat( attachee ):
	c_time = council_time()
	#1 - between 22:00 and 22:30 on council day
	#2 - between 22:30 and 23:00
	#3 - between 19:00 and 22:00
	#4 - after council events ( >23:00 and beyond that day), but without ordinary council
	#5 - ordinary council time
	#0 - otherwise
	if traders_awol() == 1:
		set_v(435,6)
	if c_time == 5:
		set_v(440,1)
	elif c_time == 1:
		if get_v(435) == 1:
			set_v(435,3)
		elif get_v(435) >= 5 or get_v(435) == 0 and get_v(440) == 0:
			set_v(440,1)
		elif get_v(435) == 4:
			#council_events()
			dummy = 1
	elif c_time == 2:
		if get_v(435) == 3 or get_v(435) == 1:
			set_v(435,4)
			set_v(436,1)
			game.global_flags[432] = 1
			#council_events()
	elif c_time == 3:
		if get_v(435) == 2:
			set_v(435,5)
			set_v(436,5)
			game.global_vars[750] = 3
			game.global_vars[751] = 3
			if (game.party[0].reputation_has(23) == 0 and (game.global_flags[814] == 0 or game.global_flags[815] == 0)):
				game.party[0].reputation_add(23)
	elif (c_time == 0 or c_time == 4):
		if c_time == 0 and game.global_flags[336] == 1 and (get_v(435) == 1 or get_v(435) == 2):
			set_v(435,0)
		elif get_v(435) == 3 or get_v(435) == 4 or (get_v(435) == 1 and c_time == 4):
		##chiefly used for the case where the whole thing played out without you
			if get_v(436) == 0:
				set_v(436,1)
			set_v(435,5)
			if (game.party[0].reputation_has(23) == 0 and (game.global_flags[814] == 0 or game.global_flags[815] == 0)):
				game.party[0].reputation_add(23)
		if npc_get(attachee, 10) == 0 and attachee.map == 5001 and c_time == 4:
			hommletans_go_home()
			npc_set(attachee,10)
		if get_v(435) == 2 and c_time == 4:
			set_v(435,5)
			set_v(436,5)
			game.global_vars[750] = 3
			game.global_vars[751] = 3
			if (game.party[0].reputation_has(23) == 0 and (game.global_flags[814] == 0 or game.global_flags[815] == 0)):
				game.party[0].reputation_add(23)
		set_v(440,0)
	return 1

def san_first_heartbeat( attachee, triggerer ):
	if (attachee.map == 5001 or attachee.map == 5069 or attachee.map == 5094 or attachee.map == 5068 or attachee.map == 5051 or attachee.map == 5095 or attachee.map == 5093 or attachee.map == 5112 or attachee.map == 5113 or attachee.map == 5062 or attachee.map == 5138 or attachee.map == 5121):	# All Worldmap Locations
		big_3_check()
		#next, check if there's a PC possessed by the new_map script, and if not - assign it to one
		scriptbearer_found = 0
		for scriptbearer in game.party:
			if scriptbearer.scripts[38] == 439:
				scriptbearer_found = 1
			if scriptbearer.scripts[38] != 439 and scriptbearer.stat_level_get( stat_hp_current ) > -10 and scriptbearer_found == 0 and scriptbearer.type == obj_t_pc:
				scriptbearer.scripts[12] = 439
				scriptbearer.scripts[38] = 439
				scriptbearer_found = 1
				
	cur_map = attachee.map
	if (cur_map == 5001 or cur_map == 5051):	# Hommlet and Nulb Exteriors
		(xx, yy) = location_to_axis(attachee.location)
		if 1000*xx + yy == 711521 or 1000*xx + yy == 497355:
			( pxx , pyy ) = location_to_axis(game.leader.location)
			if pxx == 706:
				pxx = 705
			gen_spawner = find_npc_near(game.leader, 14706)
			if gen_spawner == OBJ_HANDLE_NULL:
				# spawndude = game.obj_create(14706, location_from_axis(pxx+5,pyy+5))
				# spawndude.object_flag_unset( 2 )
				#game.particles( 'sp-Hold Person', game.party[0] )
				game.particles( 'Orb-Summon-Air-Elemental', game.party[0] )
				# spawndude.scripts[19] = 334
				# spawndude.scripts[10] = 334
				# spawndude.move(location_from_axis(pxx,pyy),0.0,0.0)

			if npc_get(attachee,11) == 0 and cur_map == 5001: # have not created extra Hommlet spawners
				attachee.object_flag_unset( OF_OFF )
				npc_set(attachee,11)
				aa = game.obj_create( 14706, location_from_axis(622, 420))
				aa.object_flag_unset( OF_OFF )
				bb = game.obj_create( 14706, location_from_axis(511, 220))
				bb.object_flag_unset( OF_OFF )
				cc = game.obj_create( 14706, location_from_axis(432, 625))
				cc.object_flag_unset( OF_OFF )

	elif (cur_map == 5016):	#Burne Tower main hall
		if game.global_flags[336] == 1 or get_v(439) & 256 != 0:
			burne = find_npc_near(attachee, 8054)
			if game.global_flags[336] == 1 and burne.leader_get() == OBJ_HANDLE_NULL:
			# destroys Burne in case he was killed outside; problems could arise
				burne.destroy()
			else:
				burne.obj_flag_set( OF_OFF )
		if game.global_flags[437] == 1 or get_v(439) & 512 != 0:
			rufus = find_npc_near(attachee, 14006)
			if game.global_flags[437] == 1 and rufus.leader_get() == OBJ_HANDLE_NULL:
				rufus.destroy()
			else:
				rufus.obj_flag_set( OF_OFF )
		if get_v(439) >= 19 or get_v(439) & 512 != 0 or get_v(439) & 256 != 0 or get_v(439) & 1024 != 0:
			for badger in game.obj_list_vicinity(attachee.location, OLC_NPC):
				if badger.name == 14371 and badger.leader_get() == OBJ_HANDLE_NULL:
					badger.obj_flag_set( OF_OFF )
	elif (cur_map == 5067):	#Temple Level 2
		bin_thr_dun_tht()
		
	elif (cur_map == 5064 or cur_map == 5140):	#Temple Int and Level 3 Lower
		give_directions()
	
	elif (cur_map == 5002):	#The Ruins of the Moathouse 
		frog_ring()

	if (attachee.map == 5069):	#Deklo Grove 
		spider_ring()

	if (attachee.map == 5094):	#Emridy Meadows 
		bear_ring()

	gcr = group_challenge_rating()
		
	if (attachee.map == 5114):	#Ogre Cave
		if (game.global_flags[411] == 0 and game.quests[53].state == 0):
			cave_orcs(attachee, gcr)
			if (gcr >= 6):
				cav_orcs_enc1(attachee)
			else:
				cav_orcs_enc(attachee, gcr)
			
	return RUN_DEFAULT
	
def Big_Three_Ambush_1(spawner):
	leader = game.party[0]
	n = 0
	while (n < 1): # Barbarian Gnolls
		npc = game.obj_create(14078, random_location(500, 491, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 2): # Gnolls
		npc = game.obj_create(14079, random_location(480, 491, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Gnolls2
		npc = game.obj_create(14080, random_location(490, 495, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Mage Sniper Crossbowmen
		npc = game.obj_create(14314, random_location(495, 495, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	npc = game.obj_create(14642, random_location(477, 489, 5)) # Slightly better archer.
	npc.turn_towards(leader)
		
	zert = game.obj_create(14045, location_from_axis( 483, 489 ))
	remove_dagger(zert)
	zert.turn_towards(leader)
	turuko = game.obj_create(14041, location_from_axis( 488, 489 ))
	remove_dagger(turuko)
	turuko.turn_towards(leader)
	kobort = game.obj_create(14028, location_from_axis( 492, 491 ))
	remove_dagger(kobort)
	kobort.turn_towards(leader)
	leader.turn_towards(kobort)
	StopCombat(kobort, 0)
	leader.begin_dialog( kobort, 600 )

def Big_Three_Ambush_2(spawner):
	leader = game.party[0]
	n = 0
	while (n < 1): # Barbarian Gnolls
		npc = game.obj_create(14078, random_location(478, 460, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 2): # Gnolls
		npc = game.obj_create(14079, random_location(475, 463, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Gnolls2
		npc = game.obj_create(14080, random_location(478, 464, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Mage Sniper Crossbowmen
		npc = game.obj_create(14314, random_location(481, 461, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	npc = game.obj_create(14642, random_location(476, 466, 5)) # Slightly better archer.
	npc.turn_towards(leader)
		
	zert = game.obj_create(14045, location_from_axis( 472, 463 ))
	remove_dagger(zert)
	zert.turn_towards(leader)
	turuko = game.obj_create(14041, location_from_axis( 475, 460 ))
	remove_dagger(turuko)
	turuko.turn_towards(leader)
	kobort = game.obj_create(14028, location_from_axis( 478, 457 ))
	remove_dagger(kobort)
	kobort.turn_towards(leader)
	leader.turn_towards(kobort)
	StopCombat(kobort, 0)
	leader.begin_dialog( kobort, 610 )

def Big_Three_Ambush_3(spawner):
	leader = game.party[0]
	n = 0
	while (n < 1): # Barbarian Gnolls
		npc = game.obj_create(14078, random_location(430, 443, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 2): # Gnolls
		npc = game.obj_create(14079, random_location(433, 453, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Gnolls2
		npc = game.obj_create(14080, random_location(431, 457, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Mage Sniper Crossbowmen
		npc = game.obj_create(14314, random_location(427, 457, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	npc = game.obj_create(14642, random_location(424, 445, 5)) # Slightly better archer.
	npc.turn_towards(leader)
		
	zert = game.obj_create(14045, location_from_axis( 427, 447 ))
	remove_dagger(zert)
	zert.turn_towards(leader)
	turuko = game.obj_create(14041, location_from_axis( 428, 451 ))
	remove_dagger(turuko)
	turuko.turn_towards(leader)
	kobort = game.obj_create(14028, location_from_axis( 430, 454 ))
	remove_dagger(kobort)
	kobort.turn_towards(leader)
	leader.turn_towards(kobort)
	StopCombat(kobort, 0)
	leader.begin_dialog( kobort, 620 )

def remove_dagger(npc):
	dagger = npc.item_find_by_proto(4060)
	while dagger != OBJ_HANDLE_NULL:
		dagger.destroy()
		dagger = npc.item_find_by_proto(4060)
	return

def money_script():
	# money script by Agetian
	game.party[0].money_adj(-game.party[0].money_get())   # strip all money
	game.party[0].money_adj(game.random_range(20000, 30000))	#add 200-300 gold pocket money
	for pc in game.party:
		# get rid of an extra longsword (4036)
		lsw = pc.item_find_by_proto(4036)
		if lsw != OBJ_HANDLE_NULL:
			lsw.destroy()
		# get rid of an extra scimitar (4045)
		scm = pc.item_find_by_proto(4045)
		if scm != OBJ_HANDLE_NULL:
			scm.destroy()

		# give money per PHB (for each class)
		if (pc.stat_level_get(stat_level_barbarian) > 0):
			pc.money_adj(game.random_range(4000, 16000))
		if (pc.stat_level_get(stat_level_bard) > 0):
			pc.money_adj(game.random_range(4000, 16000))
		if (pc.stat_level_get(stat_level_cleric) > 0):
			pc.money_adj(game.random_range(5000, 20000))
		if (pc.stat_level_get(stat_level_druid) > 0):
			pc.money_adj(game.random_range(2000, 8000))
		if (pc.stat_level_get(stat_level_fighter) > 0):
			pc.money_adj(game.random_range(6000, 24000))
		if (pc.stat_level_get(stat_level_monk) > 0):
			pc.money_adj(game.random_range(5000, 20000))
		if (pc.stat_level_get(stat_level_paladin) > 0):
			pc.money_adj(game.random_range(6000, 24000))
		if (pc.stat_level_get(stat_level_ranger) > 0):
			pc.money_adj(game.random_range(6000, 24000))
		if (pc.stat_level_get(stat_level_rogue) > 0):
			pc.money_adj(game.random_range(5000, 20000))
		if (pc.stat_level_get(stat_level_sorcerer) > 0):
			pc.money_adj(game.random_range(3000, 12000))
		if (pc.stat_level_get(stat_level_wizard) > 0):
			pc.money_adj(game.random_range(3000, 12000))
	return

def random_location(x, y, range):
	print 'Generating Location'
	if (game.random_range(1,2) == 1) :
		loc_x = ( x + game.random_range(1,range) )
	else:
		loc_x = ( x - game.random_range(1,range) )
	if (game.random_range(1,2) == 1 ):
		loc_y = ( y + game.random_range(1,range) )
	else:
		loc_y = ( y - game.random_range(1,range) )
	location = location_from_axis( loc_x, loc_y )
	return location

def is_follower(name):
	for obj in game.party:
		if (obj.name == name):
			return 1
	return 0

def bin_thr_dun_tht():
	#bin_thr_dun_tht script by rufnredde - Function: Sets ggf 286 if you have been to level 2 and resets ggf 287 enter temple blindfolded if you have entered/exited normally ggv 55
	game.global_flags[286] = 1
	if (game.global_flags[287] == 1 and game.global_vars[55] >= 2 and game.global_flags[286] == 1):
		game.global_flags[287] = 0
	return
	
def give_directions():
	#give_directions script by rufnredde - Function: Checks to make sure you have entered the temple ggv 55 other than blindfolded ggf 287 and have been to level 2 ggf 286 allows you to give directions out of the temple
	if (game.global_flags[287] == 0 and game.global_vars[55] <= 1 and game.global_flags[286] == 0):
		game.global_vars[55] = 1
	if (game.global_flags[287] == 0 and game.global_vars[55] <=1 and game.global_flags[286] == 1):
		game.global_vars[55] = game.global_vars[55] + 1
	if (game.global_flags[287] == 1 and game.global_vars[55] <= 1):
		game.global_vars[55] = game.global_vars[55] + 1
	if (game.global_flags[287] == 1) and ((game.quests[53].state == qs_completed or game.quests[53].state == qs_accepted) or (game.global_flags[996] == 1 or game.global_flags[998] == 1)):
		game.global_vars[55] = game.global_vars[55] + 2
	if (game.global_flags[287] == 1 and game.global_vars[55] >= 2):
		game.global_flags[287] = 0
	return
	
def big_3_check():
	if (game.global_flags[37] == 1) and (game.global_flags[283] == 0):
		game.global_flags[283] = 1
	return

def council_events():
### this script is fired from first_heartbeat in the Hommlet Exterior map 5001
	if ( game.leader.map == 5001 and game.global_vars[435] == 4 and game.global_flags[435] == 0 and game.global_flags[432] == 1 ):
	#### spawns everyone that was inside Towne Hall
		game.global_flags[435] = 1
	
		burne = game.obj_create(14453,location_from_axis(578,406))
		burne.scripts[10] = 0
		burne.scripts[19] = 0
		burne.obj_set_int(obj_f_critter_description_unknown,20000)
		burne.move(location_from_axis(578,406),0.0,0.0)
		burne.rotation = 1
		destroy_weapons(burne,4058,0,0)
		jaroo = game.obj_create(14005,location_from_axis(583,412))
		jaroo.scripts[10] = 0
		jaroo.scripts[19] = 0
		jaroo.obj_set_int(obj_f_critter_description_unknown,20001)
		jaroo.move(location_from_axis(583,412),0.0,0.0)
		jaroo.rotation = 0
		destroy_weapons(jaroo, 4047, 4111, 0)
		rufus = game.obj_create(14006,location_from_axis(571,407))
		rufus.scripts[10] = 0
		rufus.scripts[19] = 0
		rufus.obj_set_int(obj_f_critter_description_unknown,20002)
		rufus.move(location_from_axis(571,407),0.0,0.0)
		rufus.rotation = 3
		terjon = game.obj_create(14007,location_from_axis(570,412))
		terjon.scripts[10] = 0
		terjon.scripts[19] = 0
		terjon.obj_set_int(obj_f_critter_description_unknown,20003)
		terjon.move(location_from_axis(570,412),0.0,0.0)
		terjon.rotation = 4.5
		destroy_weapons(terjon,4124,6054,0)
		renton = game.obj_create(14012,location_from_axis(583,409))
		renton.scripts[10] = 0
		renton.scripts[19] = 0
		renton.obj_set_int(obj_f_critter_description_unknown,20007)
		renton.move(location_from_axis(583,409),0.0,0.0)
		renton.rotation = 1
		destroy_weapons(renton,4096,4036,6074)
		nevets = game.obj_create(14102,location_from_axis(576,407))
		nevets.scripts[10] = 0
		nevets.scripts[19] = 0
		nevets.obj_set_int(obj_f_critter_description_unknown,20058)
		nevets.move(location_from_axis(576,407),0.0,0.0)
		nevets.rotation = 3
		miller = game.obj_create(14031,location_from_axis(571,412))
		miller.scripts[10] = 0
		miller.scripts[19] = 0
		miller.obj_set_int(obj_f_critter_description_unknown,20026)
		miller.move(location_from_axis(571,412),3.0,0.0)
		miller.rotation = 3
		miller.condition_add_with_args( "Prone", 0, 0 )
		gundi = game.obj_create(14016,location_from_axis(582,411))
		gundi.scripts[10] = 0
		gundi.scripts[19] = 0
		gundi.obj_set_int(obj_f_critter_description_unknown,20011)
		gundi.move(location_from_axis(582,411),-10.0,-10.0)
		gundi.rotation = 3
		gundi.condition_add_with_args( "Prone", 0, 0 )
		crybaby = game.obj_create(14002,location_from_axis(575,417))
		crybaby.move(location_from_axis(575,417),0.0,0.0)
		crybaby.scripts[10] = 0
		crybaby.scripts[19] = 0
		crybaby.rotation = 5.5
		crybaby.scripts[19] = 0
		crybaby.object_flag_unset(OF_OFF)
		if (game.global_vars[436] == 4):
			game.timevent_add(proactivity,(crybaby,3000),2000)
		else:
			game.timevent_add(proactivity,(crybaby,3000),7000)

		randy1 = game.random_range(1,2)
		randy1 = 2
		#remove randomness for testing purposes
		if (randy1 == 1):
			gremag = game.obj_create(14014,location_from_axis(365,653))
			gremag.condition_add_with_args( "Invisible", 0, 0 )
			gremag.object_flag_set(OF_DONTDRAW)
			rannos = game.obj_create(14018,location_from_axis(366,655))
			rannos.condition_add_with_args( "Invisible", 0, 0 )
			rannos.object_flag_set(OF_DONTDRAW)
			dlg_popup = game.obj_create(14806,location_from_axis(364,653))


		else:
			gremag = game.obj_create(14014,location_from_axis(318,495))
			gremag.condition_add_with_args( "Invisible", 0, 0 )
			gremag.object_flag_set(OF_DONTDRAW)
			rannos = game.obj_create(14018,location_from_axis(320,496))
			rannos.condition_add_with_args( "Invisible", 0, 0 )
			rannos.object_flag_set(OF_DONTDRAW)
			dlg_popup = game.obj_create(14806,location_from_axis(317,494))
			#dlg_popup.object_flag_unset(OF_DONTDRAW)
			#dlg_popup.object_flag_unset(OF_CLICK_THROUGH)



		#### damage section, tough ones:
		if (game.global_vars[436] != 4):
			lightly_damage(renton)
			lightly_damage(terjon)
			lightly_damage(rufus)
			lightly_damage(jaroo)
			lightly_damage(burne)
			game.timevent_add(heal_script,(terjon,rufus),8500)
			game.timevent_add(heal_script,(jaroo,renton),9500)

		#### damage section, frail but important ones:
		if (game.global_vars[436] != 4):
			if (game.global_vars[436] != 6 and game.global_vars[436] != 7):
				lightly_damage(nevets)
				lightly_damage(gundi)
				game.timevent_add(heal_script,(jaroo,gundi),700)
				game.timevent_add(heal_script,(jaroo,nevets),5800)
			else:
				heavily_damage(nevets)
				heavily_damage(gundi)

		#### damage section, frail and unimportant ones:
		if (get_v(436) != 4):
			if (get_v(436) == 3):
				lightly_damage(miller)
				game.timevent_add(float_comment,(terjon,3000),3500)
				game.timevent_add(heal_script,(terjon,miller),3510)
			else:
				heavily_damage(miller)
	return


	
def frog_ring():
	# frog_ring script by rufnredde - adds Black Jay's wife's ring to Moathouse frog for neutral parties
	if ((game.party_alignment == LAWFUL_NEUTRAL or game.party_alignment == CHAOTIC_NEUTRAL or game.party_alignment == TRUE_NEUTRAL) and game.global_flags[410] == 0):
		ring_givr = game.obj_create(14460, location_from_axis( 465, 499 ))
		frogi = find_npc_near(ring_givr, 14057 )
		create_item_in_inventory( 6270, frogi )
		game.global_flags[410] = 1
		ring_givr.destroy()
	return

def bear_ring():
	# skeleton_ring script by rufnredde - adds Black Jay's wife's ring to Emridy Meadows skeleton for evil parties
	if ((game.party_alignment & ALIGNMENT_EVIL != 0) and game.global_flags[410] == 0):
		ring_givr = game.obj_create(14460, location_from_axis( 583, 511 ))
		beari = find_npc_near(ring_givr, 14053 )
		create_item_in_inventory( 6270, beari )
		game.global_flags[410] = 1
		ring_givr.destroy()
	return
	
def spider_ring():
	# spider_ring script by rufnredde - adds Black Jay's wife's ring to Deklo Grove spider for good parties
	if ((game.party_alignment == LAWFUL_GOOD or game.party_alignment == CHAOTIC_GOOD or game.party_alignment == NEUTRAL_GOOD) and game.global_flags[410] == 0):
		ring_givr = game.obj_create(14460, location_from_axis( 459, 472 ))
		spidi = find_npc_near(ring_givr, 14047 )
		create_item_in_inventory( 6270, spidi )
		game.global_flags[410] = 1
		ring_givr.destroy()
	return

def cave_orcs(spawner, gcr):
	 #cave_orcs script by rufnredde, spawner on loan from Cerulean the Blue add an ogre cave prespawn with a band of orcs
	leader = game.party[0]
	oleader = game.obj_create( 14744, location_from_axis(483, 459)) #Orc Leader
	oleader.turn_towards(leader)
	oleader.attack(leader)
	if (gcr >= 6):
		shaman = game.obj_create( 14743, location_from_axis(504, 460)) #Orc Shaman
		ass = game.obj_create( 14747, location_from_axis(509, 473)) #Half-Orc Assassin
	else:
		shaman = game.obj_create( 14748, location_from_axis(504, 460)) #Orc Shaman Lite
		ass = game.obj_create( 14751, location_from_axis(509, 473)) #Half-Orc Assassin Lite
	shaman.turn_towards(leader)
	shaman.attack(leader)
	ass.turn_towards(leader)
	ass.attack(leader)
	booty = game.obj_create( 1027, location_from_axis(485, 449)) #chest
	create_item_in_inventory( 12043, booty )
	create_item_in_inventory( 12043, booty )
	create_item_in_inventory( 12043, booty )
	create_item_in_inventory( 12040, booty )
	create_item_in_inventory( 12039, booty )
	create_item_in_inventory( 8014, booty )
	create_item_in_inventory( 8014, booty )
	create_item_in_inventory( 8014, booty )
	game.global_flags[411] = 1
	if (game.global_vars[56] < 9 and game.quests[53].state > 0):
		game.quests[62].state = qs_botched
	
	return
	
def cav_orcs_enc(spawner, gcr):
	leader = game.party[0]
	if (gcr <= 2.5):
		numb = 1
		numb2 = 2
	else:
		numb = 2
		numb2 = 3
	n = 0
	while (n < numb): #Orc Mage-Sniper Archers
		npc = game.obj_create(14750, random_location(494, 454, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < numb2): #Orc Fighters
		npc = game.obj_create(14749, random_location(489, 473, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < numb): #Orc Leader Fighter Guards
		npc = game.obj_create(14749, random_location(482, 466, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < numb): #Orc Shaman Fighter Guards
		npc = game.obj_create( 14749, random_location(501, 457, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1

	return
	
def cav_orcs_enc1(spawner):
	leader = game.party[0]
	n = 0
	while (n < 2): #Orc Mage-Sniper Archers
		npc = game.obj_create(14746, random_location(494, 454, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < 3): #Orc Fighters
		npc = game.obj_create(14745, random_location(489, 473, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < 2): #Orc Leader Fighter Guards
		npc = game.obj_create(14745, random_location(482, 466, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < 2): #Orc Shaman Fighter Guards
		npc = game.obj_create( 14745, random_location(501, 457, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
		
	return

def slave_traders(spawner):
	kenan = game.obj_create(14768, location_from_axis( 506, 586 ))
	kenan.rotation = 4.0
	tarah = game.obj_create(14769, location_from_axis( 506, 589 ))
	tarah.rotation = 4.0
	sharar = game.obj_create(14770, location_from_axis( 507, 592 ))
	sharar.rotation = 5.0
	gadham = game.obj_create(14771, location_from_axis( 509, 595 ))
	gadham.rotation = 5.0
	abaddon = game.obj_create(14772, location_from_axis( 507, 583 ))
	abaddon.rotation = 3.0
	gershom = game.obj_create(14774, location_from_axis( 503, 592 ))
	gershom.rotation = 4.0
	persis = game.obj_create(14775, location_from_axis( 509, 580 ))
	persis.rotation = 3.0
	slaveboy = game.obj_create(14776, location_from_axis( 499, 591 ))
	slaveboy.rotation = 4.0
	slaveboy = game.obj_create(14776, location_from_axis( 497, 591 ))
	slaveboy.rotation = 4.0
	slaveboy = game.obj_create(14776, location_from_axis( 497, 593 ))
	slaveboy.rotation = 5.0
	slaveboy = game.obj_create(14776, location_from_axis( 499, 593 ))
	slaveboy.rotation = 5.0
	slaveboy = game.obj_create(14776, location_from_axis( 501, 596 ))
	slaveboy.rotation = 4.0
	slaveboy = game.obj_create(14776, location_from_axis( 503, 598 ))
	slaveboy.rotation = 4.0
	kent = game.obj_create(14779, location_from_axis( 503, 596 ))
	kent.rotation = 5.0
	slavegirl = game.obj_create(14777, location_from_axis( 506, 573 ))
	slavegirl.rotation = 3.0
	slavegirl = game.obj_create(14777, location_from_axis( 504, 573 ))
	slavegirl.rotation = 3.0
	slavegirl = game.obj_create(14777, location_from_axis( 504, 575 ))
	slavegirl.rotation = 2.5
	slavegirl = game.obj_create(14777, location_from_axis( 506, 575 ))
	slavegirl.rotation = 3.0


def hommletans_go_home( ):
	moshe = game.obj_create(14460,location_from_axis(577,412))
	for npc in game.obj_list_vicinity(moshe.location, OLC_NPC):
		if (to_be_deleted_outdoors(npc) == 1):
			npc.destroy()
	moshe.destroy()
	moshe = game.obj_create(14460,location_from_axis(365,653))
	for npc in game.obj_list_vicinity(moshe.location, OLC_NPC):
		if (to_be_deleted_outdoors(npc) == 1):
			npc.destroy()
	moshe.destroy()
	moshe = game.obj_create(14460,location_from_axis(318,495))
	for npc in game.obj_list_vicinity(moshe.location, OLC_NPC):
		if (to_be_deleted_outdoors(npc) == 1):
			npc.destroy()
	moshe.destroy()
	return

def to_be_deleted_outdoors(npc):
	#8008 - Gundigoot
	#8048 - Rannos
	#8049 - Gremag
	#8054 - burne (this is the correct name)
	#14006 - Rufus
	#14031 - Miller
	#14102 - Nevets
	#14371 - badger (DO NOT DELETE THEM OUTDOORS!)
	#14806 - Script device
	#20001 - Jaroo
	#20003 - Terjon
	#20007 - Renton
	if (npc.name == 8008 or npc.name == 8048 or npc.name == 8049 or npc.name == 8054 or npc.name == 14006 or npc.name == 14031 or npc.name == 14102 or npc.name == 20001 or npc.name == 20003 or npc.name == 20007):
		if (npc.leader_get() == OBJ_HANDLE_NULL):
			return 1
	return 0

def lightly_damage(npc):
	npc.damage(OBJ_HANDLE_NULL,D20DT_POISON,dice_new("1d6"))
	npc.damage(OBJ_HANDLE_NULL,D20DT_SUBDUAL,dice_new("3d3"),D20DAP_NORMAL)
	return

def heavily_damage(npc):
	#note: this script kills an NPC
	#since the san_dying is triggered, it makes the game think you killed him
	#so to avoid problems, reduce global_vars[23] (which counts the # of Hommeletans killed) beforehand
	if (game.global_vars[23] == 0):
		flag = 0
	else:
		flag = 1
		game.global_vars[23] = game.global_vars[23] - 1
	npc.damage(OBJ_HANDLE_NULL,D20DT_POISON,dice_new("30d1"))
	npc.damage(OBJ_HANDLE_NULL,D20DT_SUBDUAL,dice_new("15d1"))
	if (flag == 0 and game.global_vars[23] > 0):
		game.global_vars[23] = game.global_vars[23] - 1
	if (game.global_vars[23] < 2 and game.party[0].reputation_has(1) == 1):
		for pc in game.party:
			pc.reputation_remove( 1 )
	return

def destroy_weapons(npc, item1, item2, item3):
	if (item1 != 0):
		moshe = npc.item_find(item1)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	if (item2 != 0):
		moshe = npc.item_find(item2)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	if (item3 != 0):
		moshe = npc.item_find(item3)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	return

def heal_script(healer, target):
	if (obj_percent_hp(target) < 100):
		if (healer.name == 20003 and target.name == 14031):
			healer.cast_spell( spell_cure_moderate_wounds, target )
		if (healer.name == 20003 and target.name == 14006):
			healer.cast_spell( spell_cure_light_wounds, target )
		if (healer.name == 20001 and target.name == 8008):
			healer.cast_spell( spell_cure_critical_wounds, target )
		if (healer.name == 20001 and target.name == 14102):
			healer.cast_spell( spell_cure_serious_wounds, target )
		if (healer.name == 20001 and target.name == 20007):
			healer.cast_spell( spell_cure_moderate_wounds, target )
		game.timevent_add(heal_script,(healer,target),700)
	return

def float_comment(attachee, line):
	attachee.float_line(line,game.leader)
	return

def proactivity(npc,line_no):
	npc.turn_towards(game.party[0])
	if (critter_is_unconscious(game.party[0]) != 1 and game.party[0].type == obj_t_pc and game.party[0].d20_query(Q_Prone) == 0 and npc.can_see(game.party[0])):
		game.party[0].begin_dialog(npc,line_no)
	else:
		for pc in game.party:
			npc.turn_towards(pc)
			if (critter_is_unconscious(pc) != 1 and pc.type == obj_t_pc and pc.d20_query(Q_Prone) == 0 and npc.can_see(pc)):
				pc.begin_dialog(npc,line_no)
	return


